package cn.trip.biz.service.impl;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripHotelRoom;
import cn.itrip.beans.pojo.ItripImage;
import cn.itrip.beans.pojo.ItripLabelDic;
import cn.itrip.beans.vo.ItripLabelDicVO;
import cn.itrip.common.RedisUtil;
import cn.itrip.dao.ItripHotelRoomMapper;
import cn.itrip.dao.ItripImageMapper;
import cn.itrip.dao.ItripLabelDicMapper;
import cn.trip.biz.service.HotleRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HotleRoomServiceImpl implements HotleRoomService {
    @Autowired
    private ItripHotelRoomMapper itripHotelRoomMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ItripImageMapper itripImageMapper;
    @Autowired
    private ItripLabelDicMapper itripLabelDicMapper;

    /**
     * 查询酒店房间列表
     *
     * @param HotelRoomMap
     * @return dto
     */
    @Override
    public Dto HotelRoomListMap(Map<String, Object> HotelRoomMap) {
        List<Date> result = null;
        Date StaryDate = null;
        StringBuffer key = new StringBuffer("hotelroom:" + HotelRoomMap.get("hotelId"));
        Date EndDate = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            StaryDate = format.parse((String) HotelRoomMap.get("startDate"));
            EndDate = format.parse((String) HotelRoomMap.get("endDate"));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Dto dto = null;
        if (HotelRoomMap.get("hotelId") == null || HotelRoomMap.get("hotelId").equals("")) {
            dto = new Dto();
            dto.setSuccess("false");
            dto.setMsg("酒店ID不能为空");
            return dto;
        } else {
            key.append("hotelId=" + HotelRoomMap.get("hotelId"));
        }
        if (HotelRoomMap.get("startDate") == null || HotelRoomMap.get("startDate").equals("") || HotelRoomMap.get("endDate") == null || HotelRoomMap.get("endDate").equals("")) {
            key.append("startDate=" + HotelRoomMap.get("startDate") + "endDate=" + HotelRoomMap.get("endDate"));
            dto = new Dto();
            dto.setSuccess("false");
            dto.setMsg("必须要填写入住时间或者退房时间");
            return dto;
        }
        if (HotelRoomMap.get("startDate") != null && !(HotelRoomMap.get("startDate").equals("")) && HotelRoomMap.get("endDate") != null && !(HotelRoomMap.get("endDate").equals(""))) {
            if (StaryDate.getTime() > EndDate.getTime()) {
                dto = new Dto();
                dto.setSuccess("false");
                dto.setMsg("入住时间不能大于退房时间");
                return dto;
            }
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(StaryDate);
            result = new ArrayList<>();
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(EndDate);
            while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
                result.add(tempStart.getTime());
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        }
        if (HotelRoomMap.get("isBook") != null && !(HotelRoomMap.get("isBook").equals(""))) {
            key.append("isBook=" + HotelRoomMap.get("isBook"));
        }
        if (HotelRoomMap.get("isCancel") != null && !(HotelRoomMap.get("isCancel").equals(""))) {
            key.append("isCancel=" + HotelRoomMap.get("isCancel"));
        }
        if (HotelRoomMap.get("isHavingBreakfast") != null && !(HotelRoomMap.get("isHavingBreakfast").equals(""))) {
            key.append("isHavingBreakfast=" + HotelRoomMap.get("isHavingBreakfast"));
        }
        if (HotelRoomMap.get("isTimelyResponse") != null && !(HotelRoomMap.get("isTimelyResponse").equals(""))) {
            key.append("isTimelyResponse=" + HotelRoomMap.get("isTimelyResponse"));
        }
        if (HotelRoomMap.get("payType") != null && !(HotelRoomMap.get("payType").equals(""))) {
            key.append("payType=" + HotelRoomMap.get("payType"));
        }
        if (HotelRoomMap.get("roomBedTypeId") != null && !(HotelRoomMap.get("roomBedTypeId").equals(""))) {
            key.append("roomBedTypeId=" + HotelRoomMap.get("roomBedTypeId"));
        }
        HotelRoomMap.put("Datelist", result);
        List<ItripHotelRoom> roomlist = null;
        List<List<ItripHotelRoom>> hotelRoomList = null;
        if (redisUtil.hasKey(key.toString())) {
            roomlist = (List<ItripHotelRoom>) redisUtil.get(key.toString());
        } else {
            roomlist = itripHotelRoomMapper.HotelRoomList(HotelRoomMap);
            redisUtil.set(key.toString(), roomlist, 600);
        }
        hotelRoomList = new ArrayList();
        for (ItripHotelRoom r : roomlist) {
            List<ItripHotelRoom> tempList = new ArrayList<>();
            tempList.add(r);
            hotelRoomList.add(tempList);
        }
        dto = new Dto();
        dto.setErrorCode("0");
        dto.setSuccess("true");
        dto.setMsg("获取房型成功");
        dto.setData(hotelRoomList);
        return dto;
    }

    /**
     * 查询酒店房间床型图片
     *
     * @param id
     * @return dto
     */
    @Override
    public Dto findimgByid(Integer id) {
        Dto dto = null;
        List<ItripImage> img = null;
        if (redisUtil.get("img:" + id) == null) {
            if (id != null && !id.equals("")) {
                try {
                    img = itripImageMapper.findimgByid(id);
                    redisUtil.set("img:" + id, img, 600);
                } catch (Exception e) {
                    dto = new Dto();
                    dto.setMsg("获取失败");
                    dto.setSuccess("false");
                    return dto;
                }
            } else {
                dto = new Dto();
                dto.setMsg("id不能为空");
                return dto;
            }
        } else {
            img = (List<ItripImage>) redisUtil.get("img:" + id);
        }
        dto = new Dto();
        dto.setSuccess("true");
        dto.setMsg("获取成功");
        dto.setData(img);
        return dto;
    }

    /**
     * 查询酒店房间床型列表
     *
     * @param id
     * @return dto
     * @throws Exception
     */
    @Override
    public List<ItripLabelDicVO> findbedByid(Long id) throws Exception {

        List<ItripLabelDicVO> bedtype = null;
        if (redisUtil.get("BedType:" + id) == null) {
            bedtype = itripLabelDicMapper.getTravelTypeByParentId(id);
            redisUtil.set("BedType:" + id, bedtype, 600);
        } else {
            bedtype = (List<ItripLabelDicVO>) redisUtil.get("BedType:" + id);
        }
        return bedtype;
    }

}
